<?php
/**
 * 管理员登陆控制器
 * @author e5
 *
 */
class LoginController extends Controller{
	public function index(){
		if (session("aname") && session("aid")) go(U("Index/index"));
		$this->display("login");
	}
	/**
	 * 管理员登陆
	 */
	public function login() {
		if (IS_POST){
			$uname = Q("post.username");
			$upwd = Q("post.password");
			if (!$uname || !$upwd) $this->error("用户名和密码都不能为空");
			$upwd = md5($upwd);
			//查询aid,apwd
			$userInfo = M("admin")->field("apwd,aid")->where("aname='{$uname}'")->find();
			if (empty($userInfo)) $this->error("用户名或密码错误");
			if ($userInfo['apwd'] != $upwd) $this->error("用户名或密码错误");
			//更新登陆时间与IP
			if (M("admin")->where("aname='{$uname}'")->update(array('logintime'=>time(),'loginip'=>$_SERVER['REMOTE_ADDR'])) != 1) $this->error("登陆失败");
			//写入session
			session("aname",$uname);
			session("aid",$userInfo['aid']);
			$this->success("登陆成功",U("Index/index"));
		}
	}
	/**
	 * 管理员退出
	 */
	public function out() {
		session(null);
		$this->success("成功退出",U("index"));
	}
}